﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HQWeb.Progect.PhotoManage
{
    public partial class FileList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindDate();
            }
        }

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //strWhere = strWhere.Replace("order by operatetime DESC", "");
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                { // when click the "GO" Button
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    //GridViewRow pagerRow = GridView1.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {  // when click the first, last, previous and next Button
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }

                // check to prevent form the NewPageIndex out of the range


                BindDate();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 编辑删除

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCommand = e.CommandName;
            string strID = e.CommandArgument.ToString();
            if (strCommand.Equals("EditData"))  //编辑
            {
                Response.Redirect("PhotoProject.aspx?id=" + strID);
            }
            else if (strCommand.Equals("UpdateData"))
            {
                Response.Redirect("VideoProject.aspx?id=" + strID);
            }
            else if (strCommand.Equals("SetUpData"))
            {
                Model.Progect.T_PUB_FILE model = new Model.Progect.T_PUB_FILE();
                BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
                model = bll.GetModel(strID);
                model.SETTOPTIME = DateTime.Now;
                bll.Update(model);
                BindDate();
                UpdateCaceData();
            }
            else if (strCommand.Equals("UnSetUpData"))
            {
                Model.Progect.T_PUB_FILE model = new Model.Progect.T_PUB_FILE();
                BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
                model = bll.GetModel(strID);
                model.SETTOPTIME = null;
                bll.Update(model);
                BindDate();
                UpdateCaceData();
            }
            else if (strCommand.Equals("Show"))
            {
                Model.Progect.T_PUB_FILE model = new Model.Progect.T_PUB_FILE();
                BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
                model = bll.GetModel(strID);
                model.PROJECTREAMRK = "1";
                bll.Update(model);
                BindDate();
                UpdateCaceData();
            }
            else if (strCommand.Equals("Hidden"))
            {
                Model.Progect.T_PUB_FILE model = new Model.Progect.T_PUB_FILE();
                BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
                model = bll.GetModel(strID);
                model.PROJECTREAMRK = "0";
                bll.Update(model);
                BindDate();
                UpdateCaceData();
            }
        }

        #endregion

        #region 绑定数据
        protected void BindDate()
        {
            string titel = Titel.Text;
            int state = int.Parse(drop_State.SelectedValue);
            if (d12.Value == "")
            {
                DateTime date = DateTime.Now;
                d12.Value = date.AddMonths(-1).ToShortDateString().Replace('/', '-');
            }
            DateTime begintime = Convert.ToDateTime(d12.Value);
            if (d13.Value == "")
            {
                DateTime date = DateTime.Now;
                d13.Value = date.ToShortDateString().Replace('/', '-');
            }
            DateTime endtime = Convert.ToDateTime(d13.Value).AddDays(1);
            int branchid = int.Parse(Session["BRANCH_ID"].ToString());
            string filetype = drop_Type.SelectedValue;
            string showmodel = DropShow.SelectedValue;
            BLL.Progect.T_PUB_FILE bll = new BLL.Progect.T_PUB_FILE();
           // endtime.AddDays(1);
            DataSet ds = new DataSet();
            ds = bll.GetFileList(titel, state, begintime, endtime, branchid, filetype, showmodel);
            //Model.SysManage.T_PUB_BRANCH branchmodel = new Model.SysManage.T_PUB_BRANCH();
            //BLL.SysManage.T_PUB_BRANCH branchbll = new BLL.SysManage.T_PUB_BRANCH();
            //branchmodel = branchbll.GetModel(branchid);
            //string ftp = "ftp://" + branchmodel.NAS_IP + "/";
            //foreach (Model.Progect.T_PUB_FILE item in modellist)
            //{
            //    item.URLOriginal = ftp + item.URLOriginal;
            //    item.UPTIME = Convert.ToDateTime(Convert.ToDateTime(item.UPTIME).ToShortDateString());
            //    //item.UPTIME.ToShortDateString();
            //}
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        #endregion

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int index = e.Row.RowIndex;
                DataRowView drv = (DataRowView)e.Row.DataItem;
                //string strState = drv.Row["ISFLAG"].ToString();
                string strType = drv.Row["TYPE"].ToString();
                string strIsShow = drv.Row["PROJECTREAMRK"].ToString();
                LinkButton btnShow = (LinkButton)e.Row.Cells[3].FindControl("LinkHidden");
                LinkButton btn2 = (LinkButton)e.Row.Cells[3].FindControl("btnEdit");
                if (strType == "0")
                {
                    btn2.CommandName = "EditData";
                }
                else if (strType == "1")
                {
                    btn2.CommandName = "UpdateData";
                }

                if (strIsShow == "0")
                {
                    btnShow.CommandName = "Show";
                    btnShow.Text = "显示";
                }
                else if (strIsShow == "1")
                {
                    btnShow.CommandName = "Hidden";
                    btnShow.Text = "隐藏";
                }
            }
        }

        protected void tbnSearch_Click(object sender, EventArgs e)
        {
            BindDate();
        }
        protected string getState(string state)
        {
            string strret = "";
            if (state == "False") strret = "未设定";
            else if (state == "True") strret = "已设定";
            return strret;
        }

        protected string getFileType(string type)
        {
            string strret = "";
            if (type == "0") strret = "图 片";
            else if (type == "1") strret = "视 频";
            return strret;
        }

        #region  更新缓存数据
        private void UpdateCaceData()
        {
            string strCachName = "Exhibition_" + Session["BRANCH_ID"].ToString();
            Universal.Caches.RemoveIdentify(strCachName);
            //Universal.Caches.InsertCommonInf("Exhibition");
        }
        #endregion

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strUrl = string.Format("../../FileManage/FileManageList.aspx");
            Response.Redirect(strUrl);
        }
    }
}